package com.tcm.controller.front;

import com.tcm.common.enums.ResponseEnum;
import com.tcm.entity.dto.param.StuUserInfoUpdateParam;
import com.tcm.entity.resp.R;
import com.tcm.service.ImageService;
import com.tcm.service.UserService;
import com.tcm.utils.AccessTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;

/**
 * 学生用户控制器
 * @author Guqier
 * @version 1.0
 * @date 2023/8/11 17:52
 */
@RestController
@RequestMapping("/stu/user")
public class StuUserController {

    @Autowired
    private UserService userService;

    @Autowired
    private ImageService imageService;

    /**
     * 获取学生用户信息
     * @param request 请求对象
     * @return 用户信息
     */
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public R getStuUserInfo(HttpServletRequest request){
        // 获取当前登录用户信息
        Long userId = AccessTokenUtil.getUserId(request);
        return R.success(userService.getStuUserInfo(userId));
    }

    /**
     * 修改学生个人信息
     * @param param 修改信息参数
     * @param request 请求对象
     * @return 响应信息
     */
    @RequestMapping(value = "/info", method = RequestMethod.PUT)
    public R modifyStuUserInfo(@RequestBody @Valid StuUserInfoUpdateParam param, HttpServletRequest request){
        boolean b = false;
        try {
            b = userService.modifyStuUserInfoSelf(param, AccessTokenUtil.getUserId(request));
        } catch (Exception e) {
            return R.fail(0, e.getMessage());
        }
        return b ? R.success() : R.fail(ResponseEnum.DATABASE_OPERATION_FAIL);
    }

    /**
     * 修改学生用户密码
     * @param param 参数Map
     * @param request http请求对象
     * @return 响应结果
     */
    @RequestMapping(value = "/pwd", method = RequestMethod.PUT)
    public R resetStuPassword(@RequestBody Map<String, String> param, HttpServletRequest request){
        String oldPassword = param.get("oldPassword");
        String newPassword = param.get("newPassword");
        if (!StringUtils.hasText(oldPassword) || !StringUtils.hasText(newPassword)){
            return R.fail(ResponseEnum.PARAM_IS_BLANK);
        }
        boolean res = userService.modifyUserPasswordSelf(oldPassword, newPassword, AccessTokenUtil.getUserId(request));
        return res ? R.success() : R.fail(0, "旧密码错误");
    }

    /**
     * 上传头像
     * @param file 文件
     * @param request http请求对象
     * @return 响应信息
     */
    @RequestMapping(value = "/avatar", method = RequestMethod.POST)
    public R updateAvatar(@RequestParam MultipartFile file, HttpServletRequest request) {
        Map<String, String> map;
        try {
            map = userService.updateStuAvatar(file, AccessTokenUtil.getUserId(request));
        } catch (Exception e) {
            return R.fail(ResponseEnum.UPLOAD_FAIL);
        }
        return map != null ? R.success(map) : R.fail(ResponseEnum.DATABASE_OPERATION_FAIL);
    }

}
